package company.maestro;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import java.util.*;

import android.os.Environment;
import android.util.Log;
import android.view.*;
import android.widget.*;

public class StudentAdapter extends ArrayAdapter<Student> 
{

  int resource;
  
  File destination;

  public StudentAdapter(Context _context, 
                             int _resource, 
                             List<Student> _items) {
    super(_context, _resource, _items);
    resource = _resource;
    
  }
  
  

  @Override
  public View getView(int studentId, View convertView, ViewGroup parent) 
  {
    LinearLayout todoView;
    

    Student student = StudentListActivity.students.get(studentId);
    
    studentId = (int)student.getId();

    String firstName 		= student.getFirstName();
    String lastName 		= student.getLastName();
    Date createdDate 		= student.getCreated();
    SimpleDateFormat sdf 	= new SimpleDateFormat("MM/dd/yy");
    String dateString 		= sdf.format(createdDate);

    if (convertView == null) 
    {
      todoView = new LinearLayout(getContext());
      String inflater = Context.LAYOUT_INFLATER_SERVICE;
      LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
      vi.inflate(resource, todoView, true);
    } 
    else 
    {
      todoView = (LinearLayout) convertView;
    }
    
    if ( StudentListActivity.LIST_TYPE == StudentListActivity.ATTENDANCE )
    {
    	todoView.setBackgroundColor(Color.GREEN);
    }
    
    
    String imageFileName = StudentListActivity.IMAGE_PREFIX + studentId + ".jpg";
    Log.e("StudentAdapter", "Reading file name: " + imageFileName);
    
	
	Bitmap userImage = null;
	try {
		userImage = StudentDBAdapter.getImageForStudent(studentId);
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    

    //TextView dateView = (TextView)todoView.findViewById(R.id.rowDate);
    TextView studentView = (TextView)todoView.findViewById(R.id.row);
    ImageView imageView = (ImageView)todoView.findViewById(R.id.studentPicture);
    
    imageView.setImageBitmap(userImage);
      
    //dateView.setText(dateString);
    studentView.setText(firstName + " " + lastName);
    studentView.setTextColor(Color.BLACK);

    return todoView;
  }
}